<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="120px">
			<el-form-item label="档案区编号" prop="areaNo">
				<el-tree-select @change="changeArea" v-model="queryParams.areaNo" :data="areaOptions"
					:props="{ value: 'archAreaNo', label: 'label', children: 'children' }" value-key="archAreaNo"
					placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案柜" prop="cabinetNo">
				<el-tree-select v-model="queryParams.cabinetNo" :data="cabinetOptions"
					:props="{ value: 'colNo', label: 'label', children: 'children' }" value-key="colNo"
					placeholder="请选择档案柜" clearable @keyup.enter="handleQuery" @click="getListArchiveCabinet" />
			</el-form-item>
			<el-form-item label="入库状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择入库状态">
					<el-option v-for="dict in stockin_status" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="盒号" prop="boxNo">
				<el-input v-model="queryParams.boxNo" placeholder="请输入盒号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案名称" prop="archiveName">
				<el-input v-model="queryParams.archiveName" placeholder="请输入档案名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案类型" prop="archiveType">
				<el-select v-model="queryParams.archiveType" placeholder="请选择档案类型">
					<el-option v-for="dict in archive_type" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="资料种类" prop="meansType">
				<el-select v-model="queryParams.meansType" placeholder="请选择资料种类">
					<el-option v-for="dict in means_type" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="对象编号" prop="archObjNo">
				<el-input v-model="queryParams.archObjNo" placeholder="请输入对象编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="对象名称" prop="archObjName">
				<el-input v-model="queryParams.archObjName" placeholder="请输入对象名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="推送状态" prop="pushStatus">
				<el-select v-model="queryParams.pushStatus" placeholder="请选择推送状态">
					<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['archive:storage:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>
		<el-table v-loading="loading" :data="storageList" @selection-change="handleSelectionChange">
			<el-table-column label="工单编号" align="center" prop="warehousingNo" width="150" />
			<el-table-column label="入库状态" align="center" prop="status">
				<template #default="scope">
					<dict-tag :options="stockin_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="操作员" align="center" prop="warehousingBy" />
			<el-table-column label="入库时间" align="center" prop="warehousingTime" width="160">
				<template #default="scope">
					<span>{{ parseTime(scope.row.warehousingTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="推送状态" align="center" prop="pushStatus">
				<template #default="scope">
					<dict-tag :options="push_status" :value="scope.row.pushStatus" />
				</template>
			</el-table-column>
			<el-table-column label="档案区编号" width="150" align="center" prop="areaNo" />
			<el-table-column label="档案柜编号" align="center" prop="cabinetNo" width="150" />
			<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
			<el-table-column label="档案盒位置" align="center" prop="storLoc" width="300" />
			<el-table-column label="借阅状态" align="center" prop="giveBackStatus">
				<template #default="scope">
					<dict-tag :options="borrow_flag" :value="scope.row.giveBackStatus" />
				</template>
			</el-table-column>
			<el-table-column label="档案名称" align="center" prop="archiveName" width="330" />
			<el-table-column label="档案类型" align="center" prop="archiveType" width="150">
				<template #default="scope">
					<dict-tag :options="archive_type" :value="scope.row.archiveType" />
				</template>
			</el-table-column>
			<el-table-column label="档案密级" align="center" prop="conLevel" width="100">
				<template #default="scope">
					<dict-tag :options="confd_lv" :value="scope.row.conLevel" />
				</template>
			</el-table-column>
			<el-table-column label="保管期限" align="center" prop="saveDate" width="100">
				<template #default="scope">
					<dict-tag :options="keep_dur" :value="scope.row.saveDate" />
				</template>
			</el-table-column>
			<el-table-column label="资料种类	" align="center" prop="meansType" width="150">
				<template #default="scope">
					<dict-tag :options="means_type" :value="scope.row.meansType" />
				</template>
			</el-table-column>
			<el-table-column label="介质类型" align="center" prop="medForm">
				<template #default="scope">
					<dict-tag :options="med_form" :value="scope.row.medForm" />
				</template>
			</el-table-column>
			<el-table-column label="对象类型" align="center" prop="archObjType" width="150">
				<template #default="scope">
					<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
				</template>
			</el-table-column>
			<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
			<el-table-column label="对象名称" align="center" prop="archObjName" />
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />

	</div>
</template>

<script setup name="Storage">
	import {
		listStorage,
		getStorage,
		delStorage,
		addStorage,
		updateStorage
	} from "@/api/archive/storage";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";

	import {
		getCurrentInstance,
		ref,
		toRefs
	} from "vue";

	const {
		proxy
	} = getCurrentInstance();
	const dateRange = ref([]);
	const storageList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const areaOptions = ref([]);
	const cabinetOptions = ref([]);

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			id: null,
			taskId: null,
			taskBoxId: null,
			custNo: null,
			custName: null,
			archName: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			warehousingNo: null,
			areaNo: null,
			cabinetNo: null,
			pushStatus: null,
			status: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
		},
		rules: {}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		push_status,
		execut_result,
		stockin_status,
		archive_type,
		save_extended,
		borrow_flag,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type
	} = proxy.useDict('push_status', 'execut_result', 'stockin_status', 'archive_type', 'save_extended', 'borrow_flag',
		'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type');
	/** 查询档案入库列表列表 */
	function getList() {
		loading.value = true;
		listStorage(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			storageList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			taskId: null,
			taskBoxId: null,
			custNo: null,
			custName: null,
			archName: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null,
			warehousingNo: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
		};
		proxy.resetForm("storageRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/storage/export', {
			...queryParams.value
		}, `入库详情记录_${new Date().getTime()}.xlsx`)
	}
	//根据档案区查询档案柜
	function changeArea() {
		cabinetOptions.value = [];
		form.value.cabinetNo = null;
		queryParams.value.cabinetNo = null;
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};
	
	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(queryq) {
		const query = {
			archiveAreaNo: form.value.areaNo || queryParams.value.areaNo
		}
		cabinetOptions.value = [];
		// 检查查询条件是否为空
		if (!query || !query.archiveAreaNo) {
			proxy.$modal.msgError("请选择档案区编号！");
			return;
		}
	
		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};
	
	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};
	
	initAreaOptions();
	getList();
</script>
